package com.example.springrabbitmq.controller;

import com.example.springrabbitmq.constant.Constants;
import com.example.springrabbitmq.model.MessageInfo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/producer")
@RestController
public class ProducerController {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("/work")
    public String work() {
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend("", Constants.WORK_QUEUE,"spring amqp work" + i);
        }
        return "发送成功";
    }

    @RequestMapping("/fanout")
    public String fanout() {
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend(Constants.FANOUT_EXCHANGE, "","spring amqp work" + i);
        }
        return "发送成功";
    }

    @RequestMapping("/direct")
    public String direct() {
        rabbitTemplate.convertAndSend(Constants.DIRECT_EXCHANGE,"orange","direct exchange orange");
        rabbitTemplate.convertAndSend(Constants.DIRECT_EXCHANGE,"black","direct exchange black");
        return "发送成功";
    }

    @RequestMapping("/topics")
    public String topics() {
        rabbitTemplate.convertAndSend(Constants.TOPICS_EXCHANGE,"red.orange.blue","direct exchange orange");
        rabbitTemplate.convertAndSend(Constants.TOPICS_EXCHANGE,"dog.cat.rabbit","direct exchange black");
        rabbitTemplate.convertAndSend(Constants.TOPICS_EXCHANGE,"lazy.outgoing.happy","direct exchange black");
        return "发送成功";
    }

    @RequestMapping("/test")
    public String test() {
        MessageInfo messageInfo = new MessageInfo();
        messageInfo.setID(123);
        rabbitTemplate.convertAndSend("","hello",messageInfo);
        return "发送成功";
    }
}
